<?php

namespace App\Models\Cruise;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class CruiseCheckTaskModel extends BaseModel
{
    use KfUid, KfSoftDeletes;

    protected $table    = 'cruise_check_tasks';
    protected $fillable = ['name', 'startTime', 'endTime', 'templateId', 'status', 'templateType', 'isDeleted'];

    const STATUS_WAIT_PUSH = 1;
    const STATUS_ING       = 2;
    const STATUS_FINISH    = 3;
    const STATUS_CANCEL    = 4;

    const STATUS_TEXT = [
        self::STATUS_WAIT_PUSH => '待推送',
        self::STATUS_ING       => '执行中',
        self::STATUS_FINISH    => '已完成',
        self::STATUS_CANCEL    => '已作废',
    ];

    /**
     * @return HasMany
     */
    public function cruiseCheckDetailRel(): HasMany
    {
        return $this->hasMany(CruiseCheckDetailModel::class, 'taskId', 'id');
    }

    public function getStatusTextAttribute(): string
    {
        return self::STATUS_TEXT[$this->status] ?? '';
    }

    /**
     * @return HasManyThrough
     */
    public function cruiseOrdersRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            CruiseOrderModel::class,
            CruiseTaskOrderMappingModel::class,
            'taskId', //mapping
            'id', // cruise_order
            'id', // task
            'orderId')->where('cruise_order.isDeleted', 0); // mapping
    }

    public function cruiseTaskGroupsRel(): HasMany
    {
        return $this->hasMany(CruiseCheckTaskGroupModel::class, 'taskId');
    }

    public static function add(string $taskName, int $templateId, ?CruiseCheckSelfTaskModel $checkSelfTaskModel = null)
    {
        $startTime = Carbon::now()->timestamp;

        $template = CruiseTemplateModel::query()->find($templateId);

        if ($template->type == CruiseTemplateModel::TYPE_3) {
            $endTime = Carbon::now()->addDays(7)->timestamp;
        } elseif ($template->type == CruiseTemplateModel::TYPE_8) {

//            // 周期的开始、结束时间戳
//            list($startT, $endT) = $checkSelfTaskModel->getRuleCycle();
//
//            $startTime = strtotime(date('Y-m-d '. $checkSelfTaskModel->executeStartHis), $startT);
//            if ($checkSelfTaskModel->executeStartHis > $checkSelfTaskModel->executeEndHis) {
//
//                // 结束时间跨天
//                // 09:00 - 01:00
//                $endTime = strtotime(date('Y-m-d '. $checkSelfTaskModel->executeEndHis, $endT)) + 86400;
//            } else {
//
//                $endTime = strtotime(date('Y-m-d '. $checkSelfTaskModel->executeEndHis, $endT));
//            }

            $startTime = $checkSelfTaskModel->executeStartHis;
            $endTime   = $checkSelfTaskModel->executeEndHis;

        } else {
            $endTime = strtotime(date('Ymd')) + 86399;
        }

        //添加任务表
        $cruiseCheckTask              = new CruiseCheckTaskModel();
        $cruiseCheckTask->name        = $taskName;
        $cruiseCheckTask->startTime   = $startTime;
        $cruiseCheckTask->endTime     = $endTime;
        $cruiseCheckTask->templateId  = $templateId;
        $cruiseCheckTask->templateType  = $template->type;
        $cruiseCheckTask->status      = CruiseCheckTaskModel::STATUS_ING;
        $cruiseCheckTask->save();

        return $cruiseCheckTask;
    }
}
